/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.web.spring.model.AdResponse;
import cn.ad.server.web.spring.model.AppflyerRequest;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.IAdserverResponse;
import cn.ad.server.web.spring.model.dactive.DactiveAdResponse;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 激活回调请求解析类
 * @author wangwei
 */

@Component("parser-appflyer")
public class AppflyerRequestParser extends HttpServletParserAdapter {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AppflyerRequestParser.class);
	/**
	 * 
	 */
	private static final String PARAM_GUID = "clickid";
	/**
	 * 
	 */
	private static final String PARAM_ANDROID_ID = "android_id";
	/**
	 * 
	 */
	private static final String PARAM_ADVERTISING_ID = "advertising_id";
	/**
	 * 
	 */
	private static final String PARAM_IDFA = "idfa";
	/**
	 * 
	 */
	private static final String PARAM_SID = "sid";
	/**
	 * 广告位标识
	 */
	private static final String PARAM_AID = "aid";
	/**
	 * 订单标识
	 */
	private static final String PARAM_CID = "cid";
	/**
	 * 
	 */
	private static final String PARAM_MID = "mid";
	/**
	 * 
	 */
	private static final String PARAM_CH = "ch";
	/**
	 * 
	 */
	private static final String PARAM_OS = "os";
	
	
	
	
	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.HttpServletParser#parse(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public IAdserverRequest parse(HttpServletRequest request) throws AdRequestParseException {
		AppflyerRequest adRquest = new AppflyerRequest();
		adRquest.setIp(IPUtils.parseIp(request));
		adRquest.setUa(request.getHeader("user-agent"));
		readRequestParam(request, adRquest);
		return adRquest;
	}
	
	/**
	 * 读取appflyer激活回调参数
	 * @param request
	 * @param appflyerRequest
	 * @throws AdRequestParseException
	 */
	public void readRequestParam(HttpServletRequest request, AppflyerRequest appflyerRequest)  throws AdRequestParseException{
		if(appflyerRequest == null){
			throw new NullPointerException("the argument appflyerRequest is null");
		}
		//clickid={guid}&android_id={android_id}&advertising_id={advertising_id}
		//&idfa={idfa}&sid={sid}&aid={aid}&cid={cid}&mid={mid}&ch={ch}&os={os}
		
		appflyerRequest.setGuid(request.getParameter(PARAM_GUID));
		appflyerRequest.setAndroid_id(request.getParameter(PARAM_ANDROID_ID));
		appflyerRequest.setAdvertising_id(request.getParameter(PARAM_ADVERTISING_ID));
		appflyerRequest.setIdfa(request.getParameter(PARAM_IDFA));
		appflyerRequest.setSid(NumberUtils.toLong(request.getParameter(PARAM_SID)));
		appflyerRequest.setAid(NumberUtils.toLong(request.getParameter(PARAM_AID)));
		appflyerRequest.setCid(NumberUtils.toLong(request.getParameter(PARAM_CID)));
		appflyerRequest.setMid(NumberUtils.toLong(request.getParameter(PARAM_MID)));
		appflyerRequest.setCh(request.getParameter(PARAM_CH));
		appflyerRequest.setOs(request.getParameter(PARAM_OS));
	}
	
	protected final ObjectMapper objectMapper = new ObjectMapper();
	
	@Override
	public void writeResponse(IAdserverRequest adRequest, IAdserverResponse adResponse, HttpServletRequest request,
			HttpServletResponse response) throws AdRequestParseException {
		try {
			if(adResponse instanceof DactiveAdResponse){
				response.setContentType("application/json;charset=utf-8");
				DactiveAdResponse res = (DactiveAdResponse)adResponse;
				AdResponse msRes = res.getAdReponse();
				ServletOutputStream outputStream = response.getOutputStream();
				objectMapper.writeValue(outputStream, msRes);
			}
		} catch (JsonGenerationException e) {
			throw new AdRequestParseException(e.getCause());
		} catch (JsonMappingException e) {
			throw new AdRequestParseException(e.getCause());
		} catch (IOException e) {
			throw new AdRequestParseException(e.getCause());
		}
		
		response.setStatus(200);
		response.setHeader("Connection", "close");
	}


}
